<?php

namespace app\admin\model\deerhome;

use think\Model;

class Items extends Model
{

    // 表名
    protected $name = 'deerhome_items';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = null;

    // 追加属性
    protected $append = [
        'status_text'
    ];

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
        self::beforeWrite(function ($row) {
            if($row->shop_in!=1){
                if($row->shop_way==1){
                    $shopList=json_decode($row->shop_list,true);
                    if(empty($shopList) || !is_array($shopList) || count($shopList)==0){
                        throw new \Exception(__('请选择门店'));
                    }
                }else{
                    $row->shop_list='[]';
                }
            }else{
                $row->shop_way=2;
                $row->shop_list='[]';
            }
        });
    }

    public function getStatusList()
    {
        return ['1' => __('Status 1'), '2' => __('Status 2')];
    }
    public function getAgentList()
    {
        return ['1' => "是", '2' => "否"];
    }
    public function getShopInList()
    {
        return ['1' => "仅上门", '2' => "仅到店", '3' => "上门+到店"];
    }
    public function getShopWayList()
    {
        return ['1' => "指定门店",'2' => "全部自营门店",'3' => "全部自营+入驻门店"];
    }
    public function getItemTypeList()
    {
        return ['1' => "上门服务"];
    }

    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function gg()
    {
        return $this->hasMany('ItemsGg','jz_items_id')->order('weigh asc');
    }
    public function cate()
    {
        return $this->belongsTo('Cate', 'cate_id','id',[],'LEFT')->setEagerlyType(0);
    }


}
